package com.bing.bingPro.controller.fiveOrder;

import com.bing.bingPro.entity.DbConnectionInfo;
import com.bing.bingPro.service.DatabaseConnector;
import com.bing.bingPro.service.impl.HiveDatabaseConnector;
import com.bing.bingPro.service.impl.MySqlDatabaseConnector;
import org.springframework.stereotype.Component;

@Component
public class DatabaseConnectionTester {
    public static boolean testDatabaseConnection(DbConnectionInfo info) {
        DatabaseConnector connector;
        switch (info.getDatabaseType().toLowerCase()) {
            case "mysql":
                connector = new MySqlDatabaseConnector();
                break;
            case "hive":
                connector = new HiveDatabaseConnector();
                break;
            default:
                throw new IllegalArgumentException("Unsupported database type: " + info.getDatabaseType());
        }
        return connector.testConnection(info);
    }

    public static void main(String[] args) {
        // 测试连接
        DbConnectionInfo info = new DbConnectionInfo();
        // 设置参数
        info.setDatabaseType("mysql");
        info.setHost("10.254.222.160");
        info.setPort(3306);
        info.setDatabaseName("jiake-asset-management");
        info.setUsername("root");
        info.setPassword("abc123456");
        // ...

        boolean isConnected = testDatabaseConnection(info);
        if (isConnected) {
            System.out.println("连接成功!");
        } else {
            System.out.println("连接失败!");
        }
    }
}
